<template>
  <el-popover placement="right-end" width="120" trigger="manual" v-model="selectionHidden">
    <el-button
      size="small"
      class="select"
      type="warning"
      @click="exportCurrentPage"
      >导出所选项</el-button
    >
    <el-button
      size="small"
      slot="reference"
      class="select"
      icon="el-icon-download"
      type="warning"
      @click="showSelection"
      >导出</el-button
    >
  </el-popover>
  <!-- <el-popover
    placement="bottom"
    title="标题"
    width="200"
    trigger="manual"
    v-model="showSelection">
    <el-button
      size="small"
      class="select"
      type="warning"
      @click="exportCurrentPage"
      >导出所选项</el-button
    >
    <el-button slot="reference" @click="showSelection">导出签名</el-button>
  </el-popover> -->
</template>
<script>
import { qmexportTableData } from "../request";
import { timestamp } from "ali-oss";
export default {
  name: "qmExport",
  props: ["config"],
  data() {
    return {
      current: 1,
      selectionHidden: false,
      // privateConfig: {
      //     url: '/deviceManager/exportExcel',
      //     method: 'POST',
      //     mineId: false,
      //     currentPage: 1,
      //     pageSize: 10,
      //     filename: '',
      //     data: {}
      // }
      privateConfig: this.config,
    };
  },
  inject: ["multipleSelection"], // 默认值
  methods: {
    showSelection(){
      this.selectionHidden = !this.selectionHidden
      console.log(this.selectionHidden);
      this.$store.commit('setqmTag', this.selectionHidden)
      console.log(this.$store.state.qmTag);
    },
    exportCurrentPage() {
      if (!this.$routeParam()) {
        this.privateConfig.currentPage = 1;
      } else {
        this.privateConfig.currentPage = this.$currentPage("page");
      }
      this.request();
    },
    exportAllPage() {
      this.privateConfig.currentPage = 1;
      // 设置最多下载条数
      this.privateConfig.pageSize = 100000;
      this.request();
    },
    request() {
      let basUrl = this.privateConfig.url
    this.privateConfig.url = `${this.privateConfig.url}`;

    this.multipleSelection.multipleSelection =  this.multipleSelection.multipleSelection.map(item => {

      if(item.flowId){
        return{
           ...item,
           id:item.flowId
        }
      }else{
        return {
          ...item
        }  
      }
       
    });

      if (this.multipleSelection) {
        let oplist = [];
        this.multipleSelection.multipleSelection.forEach((item) => {
          oplist.push(item.id);
        });
        
        let str = oplist.toString();
        console.log(str);
        this.privateConfig.url = `${this.privateConfig.url}/${str}`;
        let requestJson = this.privateConfig;
        if (this.$routeParam()) {
          requestJson.data = this.$routeParam();
        }
        if (requestJson.mineId) {
          requestJson.data.mineId = this.$store.state.mineId;
        }
        if (requestJson.companyId) { 
          requestJson.data.companyId = this.$store.state.companyId;
        }
        qmexportTableData(requestJson)
          .then((response) => {
            
            const blob = new Blob([response]);

            const elink = document.createElement("a");
            var fileName = "";
            if (this.privateConfig.filename) {
              let timestamp = new Date().getTime();
              fileName =
                this.privateConfig.filename + "_" + this.timestamp() + ".pdf";
            } else {
              fileName = "导出数据.pdf";
            }
            elink.download = fileName;
            elink.style.display = "none";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
            str = "";  
           this.privateConfig.url = basUrl
           
          })
          .catch((error) => {
             this.$message.error('导出数据失败，请检查网络!');   
          });
      } 
    },
    timestamp() {
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10
          ? "0" + new Date().getMinutes()
          : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10
          ? "0" + new Date().getSeconds()
          : new Date().getSeconds();
      return "" + yy + mm + dd + hh + mf + ss;
    },
  },
  watch: {},
  created() {},
  
};
</script>
<style></style>
